package com.sc.auction.pay.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sc.auction.common.exception.AuctionException;
import com.sc.auction.common.pojo.User;
import com.sc.auction.common.util.R;
import com.sc.auction.pay.Config.AlipayConfig;
import com.sc.auction.pay.Service.AlipayService;
import com.sc.auction.pay.feign.OrderFeignService;
import com.sc.auction.pay.feign.UserFeignService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping(value = "/pay")
@CrossOrigin
public class AlipayController {
    @Resource
    private AlipayService alipayService;

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private OrderFeignService orderFeignService;

    @GetMapping("getAliPay")
    @ApiOperation("支付宝接口")
    public void getPagePay(HttpServletResponse response,
                        @ApiParam(name = "总价",required = true) Integer totalAmount,
                        @ApiParam(name = "商品名称",required = true) String subject,
                        @ApiParam(name = "交易成功后的界面",required = true) String returnUrl,
                        @ApiParam(name = "订单号",required = true) String outTradeNo
                           ) throws Exception {

            String pay = null;

            if(outTradeNo == null){
                pay = alipayService.webPagePay(String.valueOf(new Date().getTime()),totalAmount, subject,returnUrl);
            }else{
                pay = pay = alipayService.webPagePay(outTradeNo,totalAmount, subject,returnUrl);
            }
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(pay);
            response.getWriter().flush();
            response.getWriter().close();
    }

    @GetMapping("getBalancePay")
    @ApiOperation("余额付款接口")
    public R getBalancePay(Integer userId,BigDecimal totalAmount,Integer orderId) throws AuctionException {
        return alipayService.balancePay(userId, totalAmount, orderId);
    }

    @GetMapping("returnUrl")
    public void returnUrl(HttpServletResponse rq,Double total_amount) throws IOException {
        R  r = userFeignService.getUserById(1);
        Object user1 = r.getData().get("user");
        User user = JSON.toJavaObject((JSONObject)user1,User.class);
        BigDecimal b2 = new BigDecimal(Double.valueOf(total_amount));
        user.setBalance(user.getBalance().add(b2));
        userFeignService.update(user);
        rq.sendRedirect("http://localhost:8080/personalCenter/mybalance?group=1#/personalCenter/mybalance");
    }

    @GetMapping("orderReturnUrl")
    public void orderReturnUrl(HttpServletResponse rq,String out_trade_no) throws IOException {
        orderFeignService.updateOrderStatusByOrderNo(out_trade_no,2);
        rq.sendRedirect("http://localhost:8080/personalCenter/mybalance?group=1#/personalCenter/order");
    }

}
